package net.yxsoft.controller.pc.sysmgt.infrastructure;

import net.yxsoft.enumclass.StateEnumCls;
import net.yxsoft.enumclass.WhetherEnumCls;
import net.yxsoft.controller.pc.sysmgt.common.AuthController;
import net.yxsoft.model.infrastructure.TsGroup;
import net.yxsoft.service.infrastructure.GroupService;

import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

/**
 * 组管理Controller
 * Created by leo on 2016/5/11.
 */
public class GroupController extends AuthController {

  /**
   * Service
   */
  static final GroupService groupService = GroupService.INSTANCE;

  /**
   * 主视图
   */
  public void index() {
    List<StateEnumCls> state = StateEnumCls.defaults(StateEnumCls.class);
    List<WhetherEnumCls> whether = WhetherEnumCls.COLLECTS;

    setAttr("state", state);
    setAttr("whether", whether);

    render("group.html");
  }

  //region 组管理

  /**
  * @MethodName: getGroupById
  * @Description: 根据菜单主键获取菜单信息
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:27
  * @author jhf
  */
  public void getGroupById() throws IllegalAccessException {
    renderJson(groupService.getGroupById(getPara("groupId")));
  }

  /**
  * @MethodName: saveGroup
  * @Description: 保存菜单信息
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:27
  * @author jhf
  */
  public void saveGroup() {
    renderJson(groupService.saveGroup(getModel(TsGroup.class, "", true), getLoginUser()));
  }

  /**
  * @MethodName: deleteGroupById
  * @Description: 根据主键删除菜单
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:27
  * @author jhf
  */
  public void deleteGroupById() throws SQLException {
    renderJson(groupService.deleteGroupById(getPara("groupId"), getLoginUser()));
  }

  /**
  * @MethodName: deleteGroups
  * @Description: 根据主键集合删除菜单
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:27
  * @author jhf
  */
  public void deleteGroups() throws SQLException {
    String[] groupIds = getPara("groupIds").split(",");
    renderJson(groupService.deleteGroups(groupIds, getLoginUser()));
  }

  /**
  * @MethodName: getGroupsTree
  * @Description: 获取菜单树
  * @param:
  * @return void
  * @throws
  * @date 2023/8/31 15:27
  * @author jhf
  */
  public void getGroupsTree() throws NoSuchMethodException, SecurityException, InvocationTargetException, IllegalAccessException {
    renderJson(groupService.getGroupsTree());
  }


}
